<?php
  include("includes/header.php");
  $code_get = (!isset($_GET["code"])) ? '' : $_GET["code"];
  if($code_get == "insert")
  {
    if($_FILES["gambarProject"]["name"] != "")
    {
      $nama_gambar = $_FILES["gambarProject"]["name"];
      $tmp_gambar = $_FILES["gambarProject"]["tmp_name"];
      $dir_gambar = "img/portfolio/$nama_gambar";
      move_uploaded_file($tmp_gambar, $dir_gambar);
    }
    $sql = "INSERT INTO tb_portfolio(judul,keterangan,gambar) VALUES('$_POST[namaProject]','$_POST[deskProject]','$dir_gambar')";
    $query = mysql_query($sql);
    echo "<script>window.location.href='portfolio.php';</script>";
  }
?>
<div class="col-xs-10 col-xs-offset-2 content-container">
  <div class="page-header">
    <h3>Portfolio</h3>
  </div>
  <ol class="breadcrumb">
    <li><a href="portfolio.php">Portfolio</a></li>
    <li class="active">Add</li>
  </ol>

  <form action="portfolio-add.php?code=insert" method="post" enctype="multipart/form-data" onsubmit="return cekForm()">
    <div class="form-group">
        <label>Nama Project</label>
        <input type="text" class="form-control" id="namaProject" name="namaProject">
    </div>
    <div class="form-group">
      <label>Gambar</label>
      <div class="fileupload fileupload-new" data-provides="fileupload">
        <span class="btn btn-default btn-file">
          <span class="fileupload-new">Select file</span>
          <span class="fileupload-exists">Change</span>
          <input type="file" id="gambarProject" name="gambarProject">
        </span>
        <span class="fileupload-preview"></span>
      </div>
    </div>
    <div class="form-group">
      <label>Deskripsi</label>
        <textarea class="form-control" id="deskProject" name="deskProject"></textarea>
    </div>
    <input type="submit" class="btn btn-primary" value="Submit">
  </form>
</div>
<?php
  include("includes/footer.php");
?>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Peringatan</h4>
      </div>
      <div class="modal-body">
        <input type="hidden" class="idDelete">
        <span class="isiModal"></span>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
  function cekForm()
  {
    namaProject = $('#namaProject').val();
    gambarProject = $('#gambarProject').val();
    deskProject = CKEDITOR.instances.deskProject.getData();
    if(namaProject == '' || gambarProject == '' || deskProject == '')
    {
      $('.isiModal').html('Maaf, mohon melengkapi form yang telah disediakan.');
      $('#myModal').modal('show');
      return false;
    } else
    {
      var extensions = new Array("jpg","jpeg","gif","png","bmp");
      var image_file = $('#gambarProject').val();
      var image_length = $('#gambarProject').val().length;
      var pos = image_file.lastIndexOf('.') + 1;
      var ext = image_file.substring(pos, image_length);
      var final_ext = ext.toLowerCase();
      for (i = 0; i < extensions.length; i++)
      {
        if(image_file == '' || (extensions[i]==final_ext)) 
        {
            return true;
        }
      }
      $('.isiModal').html('Maaf, ekstensi file gambar harus .jpg/.jpeg/.gif/.png/.bmp');
      $('#myModal').modal('show');
      return false;
    }
  }
  $(document).ready(function() {
    CKEDITOR.replace( 'deskProject', {
      toolbar : [
        { name: 'clipboard', groups: [ 'clipboard', 'undo' ], items: [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ] },
        { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] },
        { name: 'paragraph', groups: [ 'list', 'indent', 'align' ], items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' ] },
        { name: 'links', items: [ 'Link', 'Unlink' ] },
        { name: 'insert', items: [ 'Table', 'SpecialChar' ] },
        { name: 'styles', items: [ 'FontSize' ] },
        { name: 'colors', items: [ 'TextColor' ] }
      ]
    });
  });
</script>